/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import me.zhengjie.modules.system.domain.Clazzandstudent;
import me.zhengjie.modules.system.domain.Teachernote;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.mapper.UserMapper;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.system.service.TeachernoteService;
import me.zhengjie.modules.system.domain.vo.TeachernoteQueryCriteria;
import me.zhengjie.modules.system.mapper.TeachernoteMapper;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author shitou
* @date 2024-08-15
**/
@Service
@RequiredArgsConstructor
public class TeachernoteServiceImpl extends ServiceImpl<TeachernoteMapper, Teachernote> implements TeachernoteService {

    private final TeachernoteMapper teachernoteMapper;

    private final UserMapper userMapper;

    @Override
    public PageResult<Teachernote> queryAll(TeachernoteQueryCriteria criteria, Page<Object> page){
        if(criteria.getIsAll() != 1){
            criteria.setCreateUserId(Math.toIntExact(SecurityUtils.getCurrentUserId()));
        } else {

            criteria.setIsOpen(1);
        }

        System.out.println("=criteria="+criteria);
        IPage<Teachernote> teachernoteList = teachernoteMapper.findAll(criteria, page);

        List<Integer> allUserIds = new ArrayList<>();
        for(Teachernote teachernote:teachernoteList.getRecords()){
            allUserIds.add(teachernote.getCreateUserId());
        }

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id",allUserIds);
        List<User> userList = new ArrayList<>();
        if(allUserIds.size()!=0){
            userList = userMapper.selectList(queryWrapper);
        }


        for(Teachernote teachernote:teachernoteList.getRecords()){
            for(User user:userList){
                if(teachernote.getCreateUserId() == Math.toIntExact(user.getId())){

                    teachernote.setCreateName(user.getUsername());
                    teachernote.setIcon(user.getAvatarPath());
                }
            }
        }
        return PageUtil.toPage(teachernoteList);
    }

    @Override
    public List<Teachernote> queryAll(TeachernoteQueryCriteria criteria){

        if(criteria.getIsAll() != 1){
            criteria.setCreateUserId(Math.toIntExact(SecurityUtils.getCurrentUserId()));
        } else {

            criteria.setIsOpen(1);
        }
        return teachernoteMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Teachernote resources) {
        resources.setCreateUserId(Math.toIntExact(SecurityUtils.getCurrentUserId()));
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Teachernote resources) {
        Teachernote teachernote = getById(resources.getId());
        teachernote.copy(resources);
        resources.setCreateUserId(Math.toIntExact(SecurityUtils.getCurrentUserId()));
        saveOrUpdate(teachernote);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Teachernote> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Teachernote teachernote : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" teachid",  teachernote.getTeachId());
            map.put(" teachchapterid",  teachernote.getTeachChapterId());
            map.put(" teachsubsectionid",  teachernote.getTeachSubsectionId());
            map.put(" createuserid",  teachernote.getCreateUserId());
            map.put(" createtime",  teachernote.getCreateTime());
            map.put(" isopen",  teachernote.getIsOpen());
            map.put(" content",  teachernote.getContent());
            map.put(" coordinate",  teachernote.getCoordinate());
            map.put(" path",  teachernote.getPath());
            map.put(" othercontent",  teachernote.getOtherContent());
            map.put(" type",  teachernote.getType());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}